package com.wzw.springboot.principle.lifecycle.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 会对所有的单例bean，在初始化之前拦截做出对应的处理。实现方法一定不要返回null。
 * @author Wangzhiwen
 */
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
    /**
     * bean对象初始化方法之前执行
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor.postProcessBeforeInitialization...初始化之前执行..." + beanName);
        return bean;
    }

    /**
     * bean对象初始化方法之后执行
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor.postProcessAfterInitialization...初始化之后执行..." + beanName);
        return bean;
    }
}
